/**
 * Created by Vivienfanghua on 2016/2/16.
 */
function turntomap()
{
    $("#tab_data").setAttribute("style","display:none");
    $("#tab_map").setAttribute("style","display:block");
}
function turntodata()
{
    $("#tab_map").setAttribute("style","display:none");
    $("#tab_data").setAttribute("style","display:block");
}

function refresh()
{
    var rows=document.getElementById("datatable").rows;
    datanew=[];
    for(i=0;i<rows.length;i++)
    {
        var cells=rows[i].cells;
        var one=[];
        for(j=0;j<cells.length;j++)
        {
            var trans=parseFloat(cells[j].innerHTML);
            if(isNaN(trans))
                one.push(cells[j].innerHTML);
            else
                one.push(trans);
        }
        datanew.push(one);
    }
    dataload=datanew;
    if( Snap("#graphicgen")!=null){
        Snap("#graphicgen").remove();
    }
}

function reset()
{
    var rows=$("#datatable tbody tr");
    for(i=0;i<rows.length;i++)
    {
        if(i%2==0)
            rows[i].setAttribute("class","odd_gradeA");
        else
            rows[i].setAttribute("class","odd_gradeC");
        Snap("#Pie"+(i+1)).attr({visibility:"visible"});
    }
}

function search()
{
    var searchString=$("#searchText").val();
    if(!searchString.match(/.+\W+.+/))
    {
        alert("��ѯ�����Ƿ���");
        return;
    }
    var chars=searchString.match(/\W+/)[0];
    var index=searchString.indexOf(chars);
    var sclass=searchString.substring(0,index);
    var snum=searchString.substring(index+chars.length);
    if(snum.match(/\d+/))
        snum=parseFloat(snum);
    var rows=$("#datatable tbody td."+sclass);
    var lines=$("#datatable tbody td."+sclass).parent();

    for(one=0;one<rows.length;one++)
    {
        switch(chars)
        {
            case "==":
                if(rows[one].innerHTML==snum)
                {
                    lines.get(one).setAttribute("class","odd_gradeX");
                    Snap("#Pie"+(one+1)).attr({visibility:"visible"});
                }
                else{
                    Snap("#Pie"+(one+1)).attr({visibility:"hidden"});
                }
                break;
            case " > ":
                if(rows[one].innerHTML>snum)
                {
                    lines.get(one).setAttribute("class","odd_gradeX");
                    Snap("#Pie"+(one+1)).attr({visibility:"visible"});
                }
                else{
                    Snap("#Pie"+(one+1)).attr({visibility:"hidden"});
                }
                break;
            case "<":
                if(rows[one].innerHTML<snum)
                {
                    lines.get(one).setAttribute("class","odd_gradeX");
                    Snap("#Pie"+(one+1)).attr({visibility:"visible"});
                }
                else{
                    Snap("#Pie"+(one+1)).attr({visibility:"hidden"});
                }
                break;
            case ">=":
                if(rows[one].innerHTML>=snum)
                {
                    lines.get(one).setAttribute("class","odd_gradeX");
                    Snap("#Pie"+(one+1)).attr({visibility:"visible"});
                }
                else{
                    Snap("#Pie"+(one+1)).attr({visibility:"hidden"});
                }
                break;
            case "<=":
                if(rows[one].innerHTML<=snum)
                {
                    lines.get(one).setAttribute("class","odd_gradeX");
                    Snap("#Pie"+(one+1)).attr({visibility:"visible"});
                }
                else{
                    Snap("#Pie"+(one+1)).attr({visibility:"hidden"});
                }
                break;
        }
    }
}

function fileUploadReadText(f)
{
    if(typeof FileReader=='undefined')
    {
        alert("�������֧�֣�");
    }
    var tempFile=f[0];
    var reader=new FileReader();
    reader.readAsText(tempFile,"GBK");
    reader.onload=function(e)
    {
        dataload=readData(e.target.result,false);
        if(dataload.length>=1)
        {
            var headstring='<tr class="ui-widget-header ">';
            for(i=0;i<dataload[0].length;i++)
                headstring=headstring+'<th class='+dataload[0][i]+'>'+dataload[0][i]+'</th>';
            headstring+="</tr>";
            $("#datatable thead").append(headstring);
            for(i=1;i<dataload.length;i++)
            {
                onestring="";
                if(i%2==0)
                    onestring='<tr class="odd_gradeA">';
                else onestring='<tr class="odd_gradeC">';
                for(j=0;j<dataload[i].length;j++)
                    onestring=onestring+"<td class="+dataload[0][j]+">"+dataload[i][j]+"</td>";
                $("#datatable tbody").append(onestring);
            }
        }
        var keys = new KeyTable( {
            "table": document.getElementById('datatable')
        } );


        $('#datatable tbody td').each( function () {
            keys.event.action( this, function (nCell) {

                keys.block = true;

                $(nCell).editable( function (sVal) {

                    keys.block = false;
                    return sVal;
                }, {
                    "onblur": 'submit',
                    "onreset": function(){

                        setTimeout( function () {keys.block = false;}, 0);
                    }
                } );

                setTimeout( function () { $(nCell).click(); }, 0 );
            } );
        } );
    }
}

function download()
{
    a=document.getElementById("downloadlink");
    var win=window.open();
    win.document.write(Snap("#"+svgname).paper.toString());
}

function drawPie()
{
    if( Snap("#graphicgen")!=null){
        Snap("#graphicgen").remove();
    }
    labels=["��һ��ҵ","�ڶ���ҵ","������ҵ"];
    var ll=$("#colorset_2_title .description").text();
    var color=getColor(colormap[ll]);
    var group = Snap("#��ͼ").g();
    group.attr({id:"graphicgen"});
    var totalvalue=dataload.map(function total(d){
        return d[2]+d[3]+d[4];
    });
    totalvalue=totalvalue.slice(1);
    var rateNum=parseInt(document.getElementById("pie_rate_num").value);
    var type="";
    switch($("#pie_rate_type option:selected").text())
    {
        case "�Ȳ�ּ���":
            type="equalDiff";
            break;
        case "�������ּ���":
            type="equalNum"
    }
    var rate=Rating(totalvalue,rateNum,type);
    var sizearray=getCData(6000,10000,totalvalue);
    for(i=1;i<dataload.length;i++)
    {
        var code=dataload[i][1];
        var code_s=code.toString();
        if(code<10)
            code_s="0"+code;
        var e=document.getElementById("Prov_P"+code_s);
        var X=parseInt(e.getAttribute("cx"));
        var Y=parseInt(e.getAttribute("cy"));
        var perWidth=5000;
        var piegraph=pieChart("#"+svgname,"Pie"+i,dataload[i],X,Y,sizearray[i-1],color);
        piegraph.drag();
        group.add(piegraph);
    }
    var pielegend=pieLegend("#"+svgname,"PieLegend",60000,60000,25000,color,labels,"ͼ��",4000,dataload[0].length-2);
    pielegend.drag();
    group.add(pielegend);
}

function drawBand()
{
    if( Snap("#graphicgen")!=null){
        Snap("#graphicgen").remove();
    }
    labels=["��һ��ҵ","�ڶ���ҵ","������ҵ"];
    var ll=$("#colorset_3_title .description").text();
    var color=getColor(colormap[ll]);
    var group = Snap("#��ͼ").g();
    group.attr({id:"graphicgen"});
    var totalvalue=dataload.map(function total(d){
        return d[2]+d[3]+d[4];
    });
    totalvalue=totalvalue.slice(1);
    var rateNum=parseInt(document.getElementById("band_rate_num").value);
    var type="";
    switch($("#band_rate_type option:selected").text())
    {
        case "�Ȳ�ּ���":
            type="equalDiff";
            break;
        case "�������ּ���":
            type="equalNum"
    }
    var rate=Rating(totalvalue,rateNum,type);
    var sizearray=getCData(6000,10000,totalvalue);
    for(i=1;i<dataload.length;i++)
    {
        var code=dataload[i][1];
        var code_s=code.toString();
        if(code<10)
            code_s="0"+code;
        var e=document.getElementById("Prov_P"+code_s);
        var X=parseInt(e.getAttribute("cx"));
        var Y=parseInt(e.getAttribute("cy"));
        var perWidth=5000;
        var bandgraph=barChart("#"+svgname,"Band"+i,dataload[i],X-perWidth*dataload[i].length/2,Y,2,5000,color);
        bandgraph.drag();
        group.add(bandgraph);
    }
    var bandlegend=bandLegend("#"+svgname,"BandLegend",labels,labels.length,40000,140000,2,5000,100,0.7*Math.max.apply(null,totalvalue),color,8000);
    bandlegend.drag();
    group.add(bandlegend);
}

function drawLevel(level_color_band,level_num,level_method,data,code)
{
    var rate=Rating(data,level_num,level_method);
    var sizearray=getCData(6000,10000,totalvalue);
    for(var i=0;i<data.length;i++)
    {
        var code_i=code[i];
        var area_one=$("#"+code_i);
        if(areaone.children.length==0)
            setcolor(code_i,colormap[level_color_band][rate["type"][i-1]]);
        else
            setcolor2(code_i,colormap[level_color_band][rate["type"][i-1]]);
    }
}

//function drawColor()
//{
//    var ll=$("#colorset_title .description").text();
//    var totalvalue=dataload.map(function total(d){
//        return d[2]+d[3]+d[4];
//    });
//    totalvalue=totalvalue.slice(1);
//    var rateNum=parseInt(document.getElementById("color_num").value);
//    var type="";
//    switch($("#color_type option:selected").text())
//    {
//        case "�Ȳ�ּ���":
//            type="equalDiff";
//            break;
//        case "�������ּ���":
//            type="equalNum"
//    }
//    var rate=Rating(totalvalue,rateNum,type);
//    var sizearray=getCData(6000,10000,totalvalue);
//    for(i=1;i<dataload.length;i++) {
//        var code=dataload[i][1];
//        var code_s=code.toString();
//        if(code<10)
//            code_s="0"+code;
//        var areaone=document.getElementById(code_s);
//        if(areaone.children.length==0)
//            setcolor("Prov_R"+code_s,colormap[ll][rate["type"][i-1]]);
//        else
//            setcolor2("Prov_R"+code_s,colormap[ll][rate["type"][i-1]]);
//    }
//}

function draw()
{
    $( "#dialog" ).dialog( "open" );
    $( ".ui-widget" ).toggle();
}

function scaleout()
{
    var svg=document.getElementById("��ͼ");
    var svg2=Snap("#svgroot");
    globalscale-=0.1;
    svg2.attr({width:""+iniWidth*globalscale,height:""+iniHeight*globalscale});
    var swidth=svg.getAttribute("width");
    var sheight=svg.getAttribute("height");
    var width=parseInt(swidth.substr(0,swidth.length-1));
    var height=parseInt(sheight.substr(0,swidth.length-1));
    if(width<=10||height<=10)
    {
        alert("����������С����");
        return;
    }
    width=width-10;
    height=height-10;
    svg.setAttribute("width",width+"%");
    svg.setAttribute("height",height+"%");
}

function scalein()
{
    var svg=document.getElementById("��ͼ");
    var svg2=Snap("#svgroot");
    globalscale+=0.1;
    svg2.attr({width:""+iniWidth*globalscale,height:""+iniHeight*globalscale});
    var swidth=svg.getAttribute("width");
    var sheight=svg.getAttribute("height");
    var width=parseInt(swidth.substr(0,swidth.length-1));
    var height=parseInt(sheight.substr(0,swidth.length-1));
    if(width>=200||height>=200)
    {
        alert("�ѷŴ���������");
        return;
    }
    width=width+10;
    height=height+10;
    svg.setAttribute("width",width+"%");
    svg.setAttribute("height",height+"%");

}

function calldragmove(d) {
    d.dx += d3.event.dx;
    d.dy += d3.event.dy;
    d3.select(this)
        .attr("transform","translate("+d.dx+","+d.dy+")");
}



function panandsacle()
{
    var x = d3.scale.linear()
        .domain([0, 1500])
        .range([0, 1500]);
    var y = d3.scale.linear()
        .domain([0, 1500])
        .range([0, 1500]);
    var svg = d3.select("#"+svgname);
    var zoom = d3.behavior.zoom()		//����һ��������Ϊ
        .x(x)
        .y(y)
        .scaleExtent([0.5, 20])	//�������ŵķ�Χ
        .on("zoom", function(d){
            d3.select(this).attr("transform",
                "translate(" +d3.event.translate + ")" +	//ƽ����
                "scale(" + d3.event.scale + ")");	//������
        });
    var g=svg.select("#��ͼ");
    g.call(zoom);
}

function scaleall()
{
    d3.select("#��ͼ").attr("transform",
        "translate(0,0)" +
        "scale(1)");
}

function edit()
{
    var svg = d3.select("#"+svgname);
    var g=svg.select("#��ͼ");
    var zoom = d3.behavior.zoom()
        .on("zoom", function(d){
        });
    g.call(zoom);
}
function onloadsvg(filename,name,id)
{
    Snap.load(filename, function (svg) {
        this.appendChild(svg.node);
        var svg=Snap("#"+name);
        var widthstring=svg.attr("width");
        var heightstring=svg.attr("height");
        var iniWidth=parseFloat(widthstring.substr(0,widthstring.length-2));
        var iniHeight=parseFloat(heightstring.substr(0,heightstring.length-2));
        var scalewidth=800/iniWidth;
        var scaleheight=600/iniHeight;
        var scale=scaleheight<scalewidth?scaleheight:scalewidth;
        svg.attr({width:iniWidth*scale+"px",height:iniHeight*scale+"px"});
    }, document.getElementById(id));
}
function mapLayer(){
    // $("#layer_control").innerHTML = xmlTree(svg);
    $("#layer_control").dynatree({
        imagePath: "skin-custom/"
    });
    $('#layer_control').attr("style","visibility:visible");
    refreshcolorselect();
    $("#content").css("display","block");
    $("#bottom").css("display","block");
}
function xuanfukuang_display(){
    $("#content").css("display","none");
    $("#bottom").css("display","none");
}
function zoomfun(){
    var m = new Snap.Matrix();
    //  var scale = Math.round((0.8 + 0.4 * Math.random()) * 100) / 100;
    m.scale(1.5, 1.5,800,800);
    // ����
    Snap("g").transform(m);

    var x = d3.scale.linear()
        .domain([0, 1500])
        .range([0, 1500]);
    var y = d3.scale.linear()
        .domain([0, 1500])
        .range([0, 1500]);
    var svg = d3.select("#"+svgname);
    var zoom = d3.behavior.zoom()		//����һ��������Ϊ
        .x(x)
        .y(y)
        .scaleExtent([0.5, 20])	//�������ŵķ�Χ
        .on("zoom", function(d){
            d3.select(this).attr("transform",
                "translate(" +d3.event.translate + ")" +	//ƽ����
                "scale(" + d3.event.scale + ")");	//������
        });
    var g=svg.select("#��ͼ");
    g.call(zoom);


}
function zoomfun1(){
    var m = new Snap.Matrix();
    //  var scale = Math.round((0.8 + 0.4 * Math.random()) * 100) / 100;
    m.scale(0.5, 0.5,800,800);
    // ����
    Snap("g").transform(m);

    var x = d3.scale.linear()
        .domain([0, 1500])
        .range([0, 1500]);
    var y = d3.scale.linear()
        .domain([0, 1500])
        .range([0, 1500]);
    var svg = d3.select("#"+svgname);
    var zoom = d3.behavior.zoom()		//����һ��������Ϊ
        .x(x)
        .y(y)
        .scaleExtent([0.5, 20])	//�������ŵķ�Χ
        .on("zoom", function(d){
            d3.select(this).attr("transform",
                "translate(" +d3.event.translate + ")" +	//ƽ����
                "scale(" + d3.event.scale + ")");	//������
        });
    var g=svg.select("#��ͼ");
    g.call(zoom);


}